package Core

import (
	"Volta/App/Tcp/Route"
	"Volta/Bootstrap"
	"Volta/Core/Container"
	"Volta/Core/Coral"
	"Volta/Core/Coral/ConfigProvider"
	"Volta/Core/Coral/ElastciSearchProvider"
	"Volta/Core/Coral/LuaProvider"
	"Volta/Core/Coral/MysqlProvider"
	"Volta/Core/Coral/RedisProvider"
	"Volta/Core/Facade"
	"Volta/Core/Http"
	"Volta/Core/Proxy"
	"Volta/Core/Utils/Tools"
	"Volta/Core/gateway"
	"Volta/CronJob"
	"fmt"
	"time"
)


var Kernel kernel

type kernel struct {
	ConfigFile string
	Service string
	Name string
}


func (kernel *kernel) CoreRegister(){
	kernel.SystemServiceBind()
	{
		Facade.Register() //.Facade 自动注入容器服务
		Bootstrap.AppServiceProvider()
		kernel.Register()
	}
}
//.Volta System Service Register
func (kernel *kernel) SystemServiceBind(){
	//.Coral system service bind
	Container.App.Bind("CORAL_CONFIG",&ConfigProvider.Config{kernel.ConfigFile},"")
	Container.App.Bind("CORAL_LUA",&LuaProvider.Lua{},"")
	Container.App.Bind("CORAL_MYSQL",&MysqlProvider.Mysql{},"Connect")
	Container.App.Bind("CORAL_REDIS",&RedisProvider.Redis{},"Connect")
	Container.App.Bind("CORAL_ELASTIC",&ElasticProvider.ElasticSearch{},"Connect")
	//container.App.Bind("CORAL_OSS",&OssProvider.Oss{},"Connect")
}

//. Volta Tcp Service Register
func (kernel *kernel) TcpServiceBind(){

}

//. Volta Http Service Register
func (kernel *kernel) HttpServiceBind(){
	Container.App.Bind("GINHTTP_WORKER",&Http.HttpEngine{},"RunEngine")
}

//. Volta TcpProxy Service Register
func (kernel *kernel) TcpProxyServiceBind(){
	Container.App.Bind("TCPPROXY_WORKER",&Proxy.TcpProxy{},"Handle")
}

//. Volta Cronjob Service Register
func (kernel *kernel)CronJobServiceBind(){

}

//. Volta Services Power
func (kernel *kernel) Register(){
	kernel.start( "CORAL",Coral.Execute())
	switch kernel.Service {
	case "all":
		kernel.RunAll()
		break
	case "tcp":
		gateway.Matic(Route.RouteRegister,gateway.Options{"json"})
		break
	case "http":
		kernel.RunHttp()
		break
	case "cronjob":
		kernel.RunCronJob()
		break
	case "tcpproxy":
		kernel.RunTcpProxy()
	default:
		fmt.Println("not found %s service type",kernel.Service)
		return
	}
	kernel.Guard()
}

func (kernel *kernel) RunHttp(){
	kernel.HttpServiceBind()
	{
		kernel.start("GINHTTP",Http.Execute())
	}
	Tools.VoltaPrint("Run Http Service")
}

func (kernel *kernel) RunCronJob(){
	kernel.CronJobServiceBind()
	{
		kernel.start( "CRONJOB",CronJob.Execute())
	}
	Tools.VoltaPrint("Run Cronjob Service")
}

func (kernel *kernel) RunTcpProxy(){
	kernel.TcpProxyServiceBind()
	{
		kernel.start( "TCPPROXY",Proxy.Execute())
	}
	Tools.VoltaPrint("Run TcpProxy Service")
}

func (kernel *kernel) RunAll(){
	gateway.Matic(Route.RouteRegister,gateway.Options{"json"})
	kernel.RunCronJob()
	kernel.RunHttp()
	Tools.VoltaPrint("Run All Service")
}

func (kernel *kernel) start(appNm string,execute func(string)){
	appKeys := Container.AppKeys
	for _,v := range appKeys {
		if len(v) > len(appNm) {
			if v[0:len(appNm)] == appNm{
				execute(v)
			}
		}
	}
}

func (kernel *kernel) Guard(){
	for {
		select {
		case <- time.After(100 *time.Minute):
		}
	}
}

//.服务启动钥匙
//if License.CheckLicense(Config.App.License, Config.App.RegisterAddr) == false {
//	return
//}
//Kernel.Guard()
//func (kernel *kernel) Guard() os.Signal{
//	//.开启守护模式 常驻进程
//	c := make(chan os.Signal)
//	signal.Notify(c, os.Interrupt, os.Kill, syscall.SIGUSR1, syscall.SIGUSR2)
//	s := <-c
//	return s
//}
